#!/bin/bash
# Special thank to Shiroko, he give me the insight of coding in proper way

# Usage: $0 [arch]


################################# Here we set the configs
REALPATH=`realpath $0`
HERE_PATH=`dirname $REALPATH`
STORE_APP_PATH="$HERE_PATH"
WORKDIR=`mktemp -d`
ARCH="" # Define it later
ARCH_ANOTHERWAY="" # Define it later

PKG_NAME="firefox-spark-ace"
APP_NAME="Firefox火狐浏览器(ACE)"
#GITHUB_REPO="" #格式像这样。只在从github获取信息才使用，如果不是，则注释掉。后续下载也整合进入helper
VERSION="" # 定义版本号。或者，在后续prepare或build中定义。可使用其他两个bashimport文件中的功能
TAGS="ubuntu;deepin;uos;community" # 星火的tag信息
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Firefox最新版\n全都能用" # This should be written as json format
HOMEPAGE="https://gitee.com/spark-building-service/firefox-spark-ace"
MAINTAINER="shenmo<shenmo@spark-app.store>"



################################# Here we import our tools
if [ ! -e "/opt/bashimport/sbs-utils.bashimport" ];then
echo "Need to install spark-building-service deb first! exit"
exit 1
fi
source /opt/bashimport/sbs-utils.bashimport
source /opt/bashimport/sbs-build-helper.bashimport

##这两个里面有详尽的解释
if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="-amd64"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="-aarch64"
else
log.error "Err:Invalid Architecture"
exit 1
fi

# ===== Log 用法 =====
# log.info xxx
# log.warn xxx
# log.info xxx
# log.debug xxx
# 带颜色的echo

function prepare(){

# Below is the usable funcions

# aptss_get_update()：更新apt源并检查是否成功，如果成功则记录日志为“aptss update ok”，否则记录日志为“aptss is locked.Give up”并退出。

# get_version_from_aptss()：从aptss(全部)源中获取指定软件包的版本号。实战中建议使用下方的函数替代以避免无法预知的情况

# get_version_from_source_list_file ${PKG_NAME} ${PATH_TO_SOURCE_LIST}

# compare_version ${VER_Apt} ${VER_Upstream} (Will give the upstream version as variable $VERSION if it is higher or exit if they are the same or Ver_Apt is higher)

# get_version_from_github()：从github上获取指定仓库的最新发布版本号。需要定义GITHUB_REPO变量。会给返回值

# get_size()：获取软件包目录的大小并返回值
aptss_get_update
VER_APTSS=$(get_version_from_source_list_file "${PKG_NAME}:all" /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list)
VER_UPSTREAM=$(get_version_from_source_list_file "firefox-spark:${ARCH}" /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list)
if [ -z ${VER_APTSS} ];then
VERSION=$VER_UPSTREAM
else
compare_version ${VER_APTSS} ${VER_UPSTREAM}
fi
################################# Fill the variables below only if you use write_control function
SECTION="utils"
PRIORITY="optional"
DEPENDS="libdbus-glib-1-2"
DESCRIPTION_SHORT="一款开源自由的浏览器"
DESCRIPTION_LONG="长介绍"
PROVIDES="x-www-browser,gnome-www-browser,firefox(=${VERSION})"
REPLACES="firefox"
RECOMMENDS="speech-dispatcher"
CONFLICTS="firefox,firefox-spark(<=117.0)"
}




function build(){

pushd $STORE_APP_PATH
./create_deb_package.sh --new-pkg-name=firefox-spark-ace --orig-pkg-name=firefox-spark --version=${VERSION}

}


prepare
build
ARCH=all
write_json $STORE_APP_PATH

rm -rf $WORKDIR
